using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class LineRender : MonoBehaviour
{

	private int lengthOfLineRenderer = 2;
	private float width = 0.1f;
	public List<LineRenderer> lineList;
	

	LineRenderer lineRenderer;
	
	bool isWriteLine = false;
	
	
	void Start ()
	{
		lineRenderer = gameObject.AddComponent<LineRenderer> ();
		lineRenderer.material = new Material (Shader.Find ("Diffuse"));
		lineRenderer.material.color = Color.red;
//        lineRenderer.SetColors(Color.red, Color.red);
		lineRenderer.SetWidth (width, width);
		lineRenderer.SetVertexCount (lengthOfLineRenderer);
	}

	void Update ()
	{

		if(Input.GetMouseButtonDown(0)) {
			lineRenderer.SetPosition (0, getMouseImpactVector ());
			
			
			isWriteLine = true;
		}
		
		if(isWriteLine){
			lineRenderer.SetPosition (1, getMouseImpactVector ());
		}
		
		if(Input.GetMouseButtonDown(1)) {
			//lineRenderer.SetPosition (0, getMouseImpactVector ());	
			isWriteLine = false;
		}
		
		
	}
	
	Vector3 getMouseImpactVector ()
	{		
		Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		RaycastHit hit;

		if (Physics.Raycast (ray, out hit, Mathf.Infinity)) {
			return hit.point;
		}
	
		return Vector3.zero;
	}
}
